package com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.service;

import com.yonyou.ucf.mdf.app.isvdxq.ReturnResult.DataResult;
import com.yonyou.ucf.mdf.app.isvdxq.common.DxqConfig;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.entity.*;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.mapper.CoderfidMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.mapper.rfidCodeDetailMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.model.*;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.guize.mapper.GuiZeMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.entity.Location;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.entity.LocationEntity;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.entity.OrderNoticeEntity;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.mapper.LocationMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.mapper.OrderNoticeMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.service.LocationEntityService;
import com.yonyou.ucf.mdf.app.isvdxq.other.controller.PublicController;
import com.yonyou.ucf.mdf.app.isvdxq.other.mapper.impl.ProductMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.other.mapper.impl.WarehouseMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.entity.EpcEntity;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.entity.StorerecordEntity;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.mapper.impl.EpclistMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.mapper.impl.StorerecordEntityMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.mapper.impl.TaglistMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.service.TaglistService;
import com.yonyou.ucf.mdf.app.isvdxq.utils.MyTool;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Rfid明细Service
 */
@Service
public class RfidCodeDetailService {
    @Autowired
    rfidCodeDetailMapperImpl rfidCodeDetailMapperall;
    @Autowired
    LocationMapperImpl locationMapper;
    @Autowired
    CoderfidMapperImpl coderfidMappersel;
    @Autowired
    LocationEntityService locationEntityService;
    @Autowired
    ProductMapperImpl productMapper;

    @Autowired
    GuiZeMapperImpl guiZeMapper;
    @Autowired
    private WarehouseMapperImpl warehouseMapper;

    @Autowired
    EpclistMapperImpl tempEpclistMapper;

    @Autowired
    StorerecordEntityMapperImpl tempStorerecordEntityMapper;

    @Autowired
    OrderNoticeMapperImpl tempOrderNoticeMapper;

    @Autowired
    TaglistMapperImpl tempTaglistMapper;


    private static final Logger logger = LoggerFactory.getLogger(PublicController.class);

    //查询单据下的所有的标签数据 使用场景：1、未上架；2、已上架数据查询；3、全部数据查询；
    public DataResult selectReRFIDCodeAll(String orderid, String orderdetailid, String ordertype, String shangjiatype
            , String tenantid) {
        DataResult dataResult = null;
        List<ReRFIDCode> ReRFIDCodeList = new ArrayList<ReRFIDCode>();
        try {
            if (shangjiatype.equals("")) {
                List<StorerecordEntity> storerecordEntities = rfidCodeDetailMapperall.selectEpcRecord(orderid, orderdetailid, ordertype, tenantid);
                ReRFIDCodeList = changeDataList(storerecordEntities);
            }
            //查询未上架
            else if (shangjiatype.equals("0")) {
                List<StorerecordEntity> storerecordEntities = rfidCodeDetailMapperall.selectEpcNoupRecord(orderid, orderdetailid, ordertype, tenantid);
                ReRFIDCodeList = changeDataList(storerecordEntities);
            } else if (shangjiatype.equals("1")) {
                List<StorerecordEntity> storerecordEntities = rfidCodeDetailMapperall.selectEpcUpRecord(orderid, orderdetailid, ordertype, tenantid);
                ReRFIDCodeList = changeDataList(storerecordEntities);
            }
            if (!shangjiatype.equals("0")) {
                if (ReRFIDCodeList != null && ReRFIDCodeList.size() > 0) {
                    List<String> WarehouseidArr =
                            ReRFIDCodeList.stream().filter(u -> u.getWarehouseid() != null && !u.getWarehouseid().equals("")).map(e -> e.getWarehouseid()).distinct().collect(Collectors.toList());
                    //String WarehouseidArrS = WarehouseidArr.toString().replaceAll("\\[", "").replaceAll("\\]", "");
                    List<Location> LocationList = locationMapper.selectLocationAll(tenantid);
                    List<Warehouse> warehouseList = new ArrayList<>();
                    if (WarehouseidArr.size() > 0) {
                        warehouseList = warehouseMapper.GetwarehouseById(WarehouseidArr, tenantid);
                    }
                    for (ReRFIDCode rfidCode : ReRFIDCodeList) {
                        String LocationID = rfidCode.getLocation_id();
                        String Warehouseid = rfidCode.getWarehouseid();
                        if (!StringUtils.isEmpty(LocationID)) {
                            String locationName = getLocationNamesel(LocationID, LocationList, "");
                            rfidCode.setLocationName(locationName);
                        }
                        if (!StringUtils.isEmpty(Warehouseid)) {
                            List<Warehouse> Warehouseentry =
                                    warehouseList.stream().filter(t -> t.getId().equals(Warehouseid)).collect(Collectors.toList());
                            if (Warehouseentry != null && Warehouseentry.size() > 0) {
                                rfidCode.setWarehouseCode(Warehouseentry.get(0).getCode());
                                rfidCode.setWarehouseName(Warehouseentry.get(0).getWarehouseName());
                            }
                        }

                    }
                }
            }
            dataResult = new DataResult(1, "信息查询成功！", ReRFIDCodeList);
        } catch (Exception e) {
            // e.printStackTrace();
            dataResult = new DataResult(0, "信息查询失败！");
        }
        return dataResult;
    }

    //查询当前单子下的上架数量 分配标签数量 未分配标签的数量
    public DataResult selectReRFIDStatusCount(String orderdetailids, String tenantid) {
        DataResult dataResult = null;
        List<StorerecordEntity> ReRFIDCodeList = new ArrayList<>();
        List<RFidStatusCout> RFidStatusCoutList = new ArrayList<RFidStatusCout>();
        try {
            List<String> orderdetailidsList = new ArrayList<>();
            if (StringUtils.isNotEmpty(orderdetailids) && orderdetailids.length() > 0) {
                String[] orderdetailidString = orderdetailids.split(",");
                orderdetailidsList.addAll(Arrays.asList(orderdetailidString));
            }
            if (orderdetailidsList.size() > 0) {
                ReRFIDCodeList = rfidCodeDetailMapperall.selectReRFIDStatusCount(orderdetailidsList, tenantid);
            }
            for (String orderdetailid : orderdetailidsList) {
                RFidStatusCout rFidStatusCout = new RFidStatusCout();
                rFidStatusCout.setOrderdetailid(orderdetailid);
                //取出所有分配标签的数量
                List<StorerecordEntity> checkresultListsel = ReRFIDCodeList.stream()
                        .filter(t -> t.getOrderdetailid().equals(orderdetailid)).collect(Collectors.toList());
                if (checkresultListsel == null || checkresultListsel.size() == 0) {
                    rFidStatusCout.setShangjiaNum(0);
                    rFidStatusCout.setFenpeiCodeNum(0);
                } else {
                    Double countAll = checkresultListsel.stream().mapToDouble(it -> it.getF_stock_count()).sum();
                    //取出上架的数量
                    Double countUp = checkresultListsel.stream()
                            .filter(t -> t.getLocationId() != null && !t.getLocationId().equals(""))
                            .mapToDouble(it -> it.getF_stock_count()).sum();
                    //取出已分配标签并且未入库的数量
                    Double countNoIn = checkresultListsel.stream()
                            .filter(t -> (t.getBatch_no() == null || t.getBatch_no().equals("")))
                            .mapToDouble(it -> it.getF_stock_count()).sum();

                    rFidStatusCout.setShangjiaNum((int) Math.ceil(countUp));
                    rFidStatusCout.setFenpeiCodeNum((int) Math.ceil(countAll));
                    rFidStatusCout.setNoInCodeNum((int) Math.ceil(countNoIn));
                }
                RFidStatusCoutList.add(rFidStatusCout);
            }

            dataResult = new DataResult(1, "信息查询成功！", RFidStatusCoutList);
        } catch (Exception e) {
            dataResult = new DataResult(0, "信息查询失败！");
        }
        return dataResult;
    }

    //查询当前单子下的 发货和拣货数量 拣货数量 发货数量
    public DataResult selFaRFIDStatusCount(String orderdetailids, String tenantid) {
        DataResult dataResult = new DataResult(0, "参数异常");
        List<RFidStatusFaCout> RFidStatusCoutList = new ArrayList<RFidStatusFaCout>();
        List<String> idsList = new ArrayList<>();

        try {
            List<StorerecordEntity> ReRFIDCodeList = new ArrayList<>();
            if (orderdetailids.length() > 0) {
                String[] ordertetailidArray = orderdetailids.split(",");
                idsList.addAll(Arrays.asList(ordertetailidArray));
            }
            if (idsList.size() == 0 || idsList == null) {
                return dataResult;
            }
            ReRFIDCodeList = rfidCodeDetailMapperall.selFaRFIDStatusCount(idsList, tenantid);
            for (String orderdetailid : idsList) {
                RFidStatusFaCout rFidStatusCout = new RFidStatusFaCout();
                rFidStatusCout.setOrderdetailid(orderdetailid);
                //取出所有分配标签的数量
                final List<String> arr_status = Arrays.asList(new String[]{"4", "5", "6"});
                List<StorerecordEntity> checkresultListsel = ReRFIDCodeList.stream()
                        .filter(t -> t.getOrderdetailid().equals(orderdetailid))
                        .collect(Collectors.toList());

                //拣货数量
                Double listjianhuoNum = checkresultListsel.stream().filter(t -> t.getEpc_status().equals(4))
                        .mapToDouble(StorerecordEntity::getF_stock_count).sum();
                //发货数量
                Double listfahuoNum = checkresultListsel.stream().filter(t -> t.getEpc_status() == 6)
                        .mapToDouble(StorerecordEntity::getF_stock_count).sum();
                //配货数量
                Double listAllotNum = checkresultListsel.stream().filter(t -> t.getEpc_status().equals(5))
                        .mapToDouble(StorerecordEntity::getF_stock_count).sum();

                if (listjianhuoNum != null) {
                    rFidStatusCout.setJianhuoNum(listjianhuoNum);
                } else {
                    rFidStatusCout.setJianhuoNum(0.0);
                }
                if (listfahuoNum != null) {
                    rFidStatusCout.setFahuoNum(listfahuoNum);
                } else {
                    rFidStatusCout.setFahuoNum(0.0);
                }
                if (listAllotNum != null) {
                    rFidStatusCout.setAllotNum(listAllotNum);
                } else {
                    rFidStatusCout.setFahuoNum(0.0);
                }
                rFidStatusCout.setFaAndjianhuoNum(listjianhuoNum + listfahuoNum + listAllotNum);
                RFidStatusCoutList.add(rFidStatusCout);
            }
            dataResult = new DataResult(1, "信息查询成功！", RFidStatusCoutList);
        } catch (Exception e) {
            //e.printStackTrace();
            dataResult = new DataResult(0, "信息查询失败！");
        }
        return dataResult;
    }

    private List<ReRFIDCode> changeDataList(List<StorerecordEntity> pickingDetailRecord) {
        List<ReRFIDCode> resultList = new ArrayList<>();
        for (StorerecordEntity item : pickingDetailRecord) {
            ReRFIDCode reRFIDCode = new ReRFIDCode();
            reRFIDCode.setWarehouseid(item.getWarehouse_id());
            reRFIDCode.setWarehouseCode(item.getWarehouse_code());
            reRFIDCode.setLocationName(item.getWarehouse_name());
            reRFIDCode.setBatch_no(item.getBatch_no());
            reRFIDCode.setBatchno(item.getBatchno());
            reRFIDCode.setCreator(item.getCreator());
            reRFIDCode.setRfidcodeId(item.getRfidcodeId());
            reRFIDCode.setRfid_code(item.getRfidcode());
            reRFIDCode.setRfid_status(String.valueOf(item.getEpc_status()));
            reRFIDCode.setEpc_status(String.valueOf(item.getEpc_status()));
            reRFIDCode.setEpc_tid(item.getEpc_tid());
            reRFIDCode.setF_stock_count(item.getF_stock_count());
            reRFIDCode.setfStockCount(item.getF_stock_count());
            reRFIDCode.setId(item.getId());
            reRFIDCode.setProductDate(item.getProductDate());
            reRFIDCode.setInvalidDate(item.getInvalidDate());
            reRFIDCode.setWarehouseid(item.getWarehouse_id());
            reRFIDCode.setWarehouseCode(item.getWarehouse_code());
            reRFIDCode.setWarehouseName(item.getWarehouse_name());
            reRFIDCode.setProduct_id(item.getProduct_id());
            reRFIDCode.setProduct_sku_id(item.getProduct_sku_id());
            reRFIDCode.setOrderid(item.getOrderid());
            reRFIDCode.setOrderdetailid(item.getOrderdetailid());
            reRFIDCode.setLocation_id(item.getLocationId());
            resultList.add(reRFIDCode);
        }
        return resultList;
    }

    //返回出库单拣货信息(添加返回拣货货位信息)
    public DataResult selectPickingDetail(String orderid, String tenantid) {
        DataResult dataResult = new DataResult(0, "参数异常");
        try {
            List<ReRFIDCode> ReRFIDCodeList = new ArrayList<>();
            List<StorerecordEntity> pickingDetailRecord = rfidCodeDetailMapperall.getPickingDetailRecord(orderid, tenantid);
            if (pickingDetailRecord != null && pickingDetailRecord.size() > 0) {
                List<Location> LocationList = locationMapper.selectLocationAll(tenantid);
                ReRFIDCodeList = changeDataList(pickingDetailRecord);
                for (ReRFIDCode item : ReRFIDCodeList) {
                    String LocationID = item.getLocation_id();
                    if (!StringUtils.isEmpty(LocationID)) {
                        String locationName = getLocationNamesel(LocationID, LocationList, "");
                        item.setLocationName(locationName);
                    }
                }
            }
            dataResult = new DataResult(1, "信息查询成功！", ReRFIDCodeList);
        } catch (Exception e) {
            dataResult.setMessage(e.getMessage());
        }
        return dataResult;
    }

    //拣货根据物资id和物资skuid获取当前货架信息
    public DataResult GetlocaltionH(String product_id, String product_sku_id, String warehouse_id,
                                    String batchno, String count, String tenantid) {
        DataResult dataResult = new DataResult(0, "参数异常");
        try {
            batchno = batchno == null ? "" : batchno;
            if (StringUtils.isEmpty(count) || count.equals("0")) return dataResult;
            double stockCount = Double.parseDouble(count);
            Double tempCount = 0.0;
            List<CoderfidViewBean> myList = new ArrayList<>();
            List<CoderfidViewBean> list_coderfid = coderfidMappersel.SelcoderfidListBylo(product_id, product_sku_id,
                    warehouse_id, batchno, tenantid);
            List<CoderfidViewBean> tempList1 = list_coderfid.stream()
                    .filter(it -> StringUtils.isNotEmpty(it.getInvalidDate()))
                    .collect(Collectors.toList());
            List<String> locationIds = tempList1.stream().map(it -> it.getLocation_id())
                    .distinct().collect(Collectors.toList());

            for (String locationId : locationIds) {
                Double tempCountLocationId = 0.0;
                if (tempCount >= stockCount) break;
                CoderfidViewBean entity = myList.stream().filter(it -> it.getLocation_id().equals(locationId))
                        .findAny().orElse(null);
                if (entity == null) {
                    entity = tempList1.stream().filter(it -> it.getLocation_id().equals(locationId))
                            .findAny().orElse(null);
                    tempCountLocationId = tempList1.stream().filter(it -> it.getLocation_id().equals(locationId))
                            .mapToDouble(CoderfidViewBean::getF_stock_count)
                            .sum();
                    tempCount += tempCountLocationId;
                    String epcs = tempList1.stream().filter(it -> it.getLocation_id().equals(locationId))
                            .map(it -> it.getRfid_code())
                            .collect(Collectors.joining(","));
                    entity.setRfid_code(epcs);
                    entity.setF_stock_count(tempCountLocationId);
                    myList.add(entity);
                }
            }
            // 无期效管理
            if (tempCount < stockCount) {
                List<String> tempIds = tempList1.stream().map(it -> it.getId()).collect(Collectors.toList());
                List<CoderfidViewBean> tempList2 = list_coderfid.stream()
                        .filter(it -> !tempIds.contains(it.getId()))
                        .collect(Collectors.toList());
                List<String> locationIds2 = tempList2.stream().map(it -> it.getLocation_id())
                        .distinct().collect(Collectors.toList());
                for (String locationId : locationIds2) {
                    Double tempCountLocationId = 0.0;
                    if (tempCount >= stockCount) break;
                    CoderfidViewBean entity = myList.stream().filter(it -> it.getLocation_id().equals(locationId))
                            .findAny().orElse(null);
                    if (entity == null) {
                        entity = tempList2.stream().filter(it -> it.getLocation_id().equals(locationId))
                                .findAny().orElse(null);
                        tempCountLocationId = tempList2.stream().filter(it -> it.getLocation_id().equals(locationId))
                                .mapToDouble(CoderfidViewBean::getF_stock_count)
                                .sum();
                        tempCount += tempCountLocationId;
                        String epcs = tempList2.stream().filter(it -> it.getLocation_id().equals(locationId))
                                .map(it -> it.getRfid_code())
                                .collect(Collectors.joining(","));
                        entity.setRfid_code(epcs);
                        entity.setF_stock_count(tempCountLocationId);
                        myList.add(entity);
                    }
                    // 已有数据添加
                    else {
                        int i = myList.indexOf(entity);
                        tempCountLocationId = tempList2.stream().filter(it -> it.getLocation_id().equals(locationId))
                                .mapToDouble(CoderfidViewBean::getF_stock_count)
                                .sum();
                        tempCountLocationId = entity.getF_stock_count() + tempCountLocationId;
                        myList.get(i).setF_stock_count(tempCountLocationId);
                    }
                }
            }
            if (myList.size() > 0) {
                List<String> ids_warehouse = myList.stream().map(it -> it.getWarehouse_id()).distinct()
                        .collect(Collectors.toList());
                if (ids_warehouse.size() == 0) return dataResult;
                List<LocationEntity> list_Location = locationEntityService.getLocationByWhIds(ids_warehouse, tenantid);
                for (CoderfidViewBean item : myList) {
                    String LocationID = item.getLocation_id();
                    if (!StringUtils.isEmpty(LocationID)) {
                        LocationEntity locationEntity = list_Location.stream()
                                .filter(it -> it.getId().equals(LocationID))
                                .findFirst().orElse(null);
                        String locationName = TaglistService.GetFullPathnameByEntity(locationEntity, list_Location);
                        item.setLocationName(locationName);
                    }
                }
            }
            dataResult = new DataResult(1, "信息查询成功！");
            dataResult.setDataList(myList);
        } catch (Exception e) {
            //e.printStackTrace();
            dataResult = new DataResult(0, "信息查询失败！");
        }
        return dataResult;
    }

    //获取到货单的分配标签信息
    public DataResult getDaohuoDetailCount(String orderid, String tenantid, String userId) {
        DataResult dataResult = null;
        try {
            List<DaohuoList> dataList = new ArrayList<>();
            List<StorerecordEntity> recordList = rfidCodeDetailMapperall.getDaohuoListRecord(orderid, tenantid, userId);
            int size = recordList.stream().map(it -> it.getBatchno_dxq()).distinct().collect(Collectors.toList()).size();
            if (size != 1) {
                dataResult = new DataResult(0, "数据批次异常");
                return dataResult;
            }
            String batchno_dxq = recordList.get(0).getBatchno_dxq();
            for (StorerecordEntity item : recordList) {
                DaohuoList daohuo = new DaohuoList();
                daohuo.setBatchno(item.getBatchno());
                daohuo.setBatchno_dxq(item.getBatchno_dxq());
                daohuo.setF_stock_count(item.getF_stock_count());
                daohuo.setRfid_code(item.getRfidcode());
                daohuo.setId(item.getRfidcodeId());
                daohuo.setRecordid(item.getId());
                daohuo.setOrderid(item.getOrderid());
                daohuo.setOrdercode(item.getOrdercode());
                daohuo.setOrderdetailid(item.getOrderdetailid());
                daohuo.setProduct_id(item.getProduct_id());
                daohuo.setProduct_sku_id(item.getProduct_sku_id());
                daohuo.setProductDate(item.getProductDate());
                daohuo.setWarehouse_id(item.getWarehouse_id());
                daohuo.setWarehouse_code(item.getWarehouse_code());
                daohuo.setWarehouse_name(item.getWarehouse_name());

                dataList.add(daohuo);
            }
            dataResult = new DataResult(1, "信息查询成功！", dataList);
            dataResult.setData(batchno_dxq);
        } catch (Exception e) {
            e.printStackTrace();
            dataResult = new DataResult(0, "信息查询失败！");
        }
        return dataResult;
    }

    //手持机推送接口到用友，用友会写本地数据
    @Transactional(rollbackFor = Exception.class)
    public DataResult UpdateInStockParams(List<PutInStock> PutInStockList, String tenantid) {
        DataResult dataResult = new DataResult(0, "旧功能不再支持");
//        SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
//        CoderfidMapper coderfidMappersel = session.getMapper(CoderfidMapper.class);
//        rfidCodeDetailMapper rfidCodeDetailMapperall = session.getMapper(rfidCodeDetailMapper.class);
//        try {
//            if (PutInStockList != null && PutInStockList.size() > 0) {
//                //生成批次号
//                String picicode = MyTool.GenerateBatchnoDXQ(PutInStockList.get(0).getOrdertype());
//                for (int i = 0; i < PutInStockList.size(); i++) {
//                    //当前获取的是dxq_storerecord的主键集合
//                    List<String> storerecordIds =
//                            coderfidMappersel.SelcoderfidByparam(PutInStockList.get(i).getBatchno(),
//                                    PutInStockList.get(i).getProductDate(), PutInStockList.get(i).getOrderdetailid(),
//                                    PutInStockList.get(i).getOrdertype(), tenantid)
//                                    .stream().map(e -> e.getId()).collect(Collectors.toList());
//                    //当前获取的是dxq_coderfid的主键集合
//                    List<String> coderfidIdlist =
//                            coderfidMappersel.SelcoderfidByidparam(PutInStockList.get(i).getBatchno(),
//                                    PutInStockList.get(i).getProductDate(), PutInStockList.get(i).getOrderdetailid(),
//                                    PutInStockList.get(i).getOrdertype(), tenantid)
//                                    .stream().map(e -> e.getId()).collect(Collectors.toList());
//                    if (coderfidIdlist != null && coderfidIdlist.size() > 0) {
//                        coderfidMappersel.UpdaterfidinvalidDate(coderfidIdlist,
//                                PutInStockList.get(i).getInvaliddate(), PutInStockList.get(i).getModifier(), tenantid);
//                    }
//                    //增加回写完善信息stockUnitId 不同物料同时回写不能批量回写
//                    if (storerecordIds != null && storerecordIds.size() > 0) {
//                        String invalidDate = PutInStockList.get(i).getInvaliddate();
//
//                        rfidCodeDetailMapperall.updateInstock(storerecordIds, picicode,
//                                PutInStockList.get(i).getSourceid(), PutInStockList.get(i).getSourceautoid(),
//                                PutInStockList.get(i).getModifier(), PutInStockList.get(i).getSource(),
//                                PutInStockList.get(i).getFirstsource(), PutInStockList.get(i).getFirstsourceautoid(),
//                                PutInStockList.get(i).getFirstsourceid(), PutInStockList.get(i).getUpcode(),
//                                PutInStockList.get(i).getFirstupcode(), PutInStockList.get(i).getPoid(),
//                                PutInStockList.get(i).getPodetailid(), PutInStockList.get(i).getPocode(),
//                                tenantid, StringUtils.isEmpty(invalidDate) ? null : invalidDate);
//                    }
//                    if (i % 500 == 499) {//每500条提交一次防止内存溢出
//                        session.commit();
//                        session.clearCache();
//                    }
//                }
//                session.commit();
//                session.clearCache();
//                // coderfidMappersel.Updatechuku(rfidcodeIdArr, PurOutList.get(0).getCreator(), "6");
//                dataResult = new DataResult(1, "数据入库成功");
//            } else {
//                dataResult = new DataResult(1, "数据入库成功条数为0");
//            }
//        } catch (Exception e) {
//            //e.printStackTrace();
//            session.clearCache();
//            dataResult = new DataResult(0, "数据入库失败！");
//            return dataResult;
//        }

        return dataResult;

    }

    //根据出库详情id更新状态
    @Transactional(rollbackFor = Exception.class)
    public DataResult UpdateOutStatus(List<PurOut> PurOutList, String tenantid, String pickbatchno) {
        DataResult dataResult = null;
        List<String> storerecordIdList = new ArrayList<>();
        try {
            if (PurOutList != null && PurOutList.size() > 0) {
                String modifier = PurOutList.get(0).getModifier();
                //生成批次号
                String picicode = MyTool.GenerateBatchnoDXQ(PurOutList.get(0).getOrdertype());
                for (int i = 0; i < PurOutList.size(); i++) {
                    rfidCodeDetailMapperall.updateOut(PurOutList.get(i).getId(), picicode,
                            PurOutList.get(i).getSourceid(), PurOutList.get(i).getSourceautoid(),
                            PurOutList.get(i).getCreator(), PurOutList.get(i).getSource(),
                            PurOutList.get(i).getFirstsource(), PurOutList.get(i).getFirstsourceautoid(),
                            PurOutList.get(i).getFirstsourceid(), PurOutList.get(i).getUpcode(),
                            PurOutList.get(i).getFirstupcode(), PurOutList.get(i).getPoid(),
                            PurOutList.get(i).getPodetailid(), PurOutList.get(i).getPocode(), tenantid);
                    if (i % 500 == 499) {//每500条提交一次防止内存溢出

                    }
                    storerecordIdList.add(PurOutList.get(i).getId());
                }
//                coderfidMappersel.Updatechuku(rfidcodeIdlist, PurOutList.get(0).getCreator(), "6", tenantid);
                List<String> rfidcodeIdlist = PurOutList.stream().map(e -> e.getRfidcodeId())
                        .collect(Collectors.toList());
                // 清理标签
                coderfidMappersel.UpdatechukuDel(rfidcodeIdlist, tenantid);
                List<EpcEntity> dataList = tempTaglistMapper.selectInfoByEpcIds(rfidcodeIdlist, tenantid);
                if (dataList.size() > 0) {
                    List<String> epc_tag = dataList.stream().map(it -> it.getRfid_code()).collect(Collectors.toList());
                    // 清理托盘
                    int rInt_tag = tempTaglistMapper.deleteByEpcKeys(epc_tag, tenantid);
                }
                if (!StringUtils.isEmpty(pickbatchno)) {
                    String orderid = PurOutList.get(0).getSourceid();
                    tempOrderNoticeMapper.EditEntityStatusForPick(orderid, DxqConfig.AllotOk, 0, pickbatchno, DxqConfig.StatusOk, modifier, tenantid);
                }
                dataResult = new DataResult(1, "数据发货成功");
                dataResult.setData(storerecordIdList);
            } else {
                dataResult = new DataResult(1, "数据发货成功条数为0");
            }
        } catch (Exception e) {
            //e.printStackTrace();
            dataResult = new DataResult(0, "数据发货失败！");
            return dataResult;
        }

        return dataResult;

    }

    /**
     * 获取位置的绝对路径
     *
     * @param locationID   货位Id
     * @param LocationList 相关的货位列表
     * @param locationName 货位名称
     * @return 货位名称
     * @throws Exception 异常抛出
     */
    public String getLocationNamesel(String locationID, List<Location> LocationList, String locationName) throws Exception {
        try {
            Location location = LocationList.stream().filter(u -> u.getId().equals(locationID)).findAny().orElse(null);
            if (location != null) {
                locationName = location.getLocationName() + locationName;
                String locationpar = location.getParent_id();
                if (locationpar != null) {
                    return getLocationNamesel(locationpar, LocationList, locationName);
                } else {
                    String warehouseName = location.getWarehouse_name();
                    return warehouseName + locationName;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return locationName;
    }

    //标签绑定-采购入库单明细绑定RFID标签
    public String BindCaigouRfidCode(String locationID, List<Location> LocationList, String locationName) throws Exception {
        try {
            Location location = LocationList.stream().filter(u -> u.getId().equals(locationID)).findAny().orElse(null);
            if (location != null) {
                locationName = location.getLocationName() + locationName;
                String locationpar = location.getParent_id();
                if (locationpar != null) {
                    return getLocationNamesel(locationpar, LocationList, locationName);
                } else {
                    return locationName;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return locationName;
    }

    /**
     * 绑定rfid标签 重写逻辑 批量写入
     * editlast: 2023-02-01 by chen
     *
     * @param CodeBindList
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public DataResult BindRfidCode(List<RfidCodebind> CodeBindList, String batchNum, String inbatchno) {
        DataResult dataResult = new DataResult(0, "参数异常");
        if (CodeBindList == null || CodeBindList.size() == 0) {
            dataResult = new DataResult(0, "提交的数据条数为空!");
            return dataResult;
        }
        try {
            List<EpcEntity> list_epc = new ArrayList<>();
            List<StorerecordEntity> list_StorerecordEntity = new ArrayList<>();
            for (RfidCodebind CodeBind : CodeBindList) {
                String epcid = MyTool.getRandomId();
                EpcEntity epcEntity = new EpcEntity();
                epcEntity.setId(epcid);
                epcEntity.setRfid_status(0);
                epcEntity.setRfid_code(CodeBind.getRfid_code());
                if (!StringUtils.isEmpty(CodeBind.getBatchno()))
                    epcEntity.setBatchno(CodeBind.getBatchno());
                epcEntity.setEpc_tid(CodeBind.getEpc_tid());
                epcEntity.setModifier(CodeBind.getModifier());
                epcEntity.setCreator(CodeBind.getCreator());
                epcEntity.setModify_time(MyTool.GenerateTime());
                epcEntity.setCreate_time(MyTool.GenerateTime());
                epcEntity.setYtenant_id(CodeBind.getTenant_id());
                epcEntity.setWarehouse_id(CodeBind.getWarehouse_id());
                epcEntity.setWarehouse_code(CodeBind.getWarehouse_code());
                epcEntity.setProduct_id(CodeBind.getProduct_id());
                epcEntity.setProduct_name(CodeBind.getProduct_name());
                epcEntity.setProduct_sku_id(CodeBind.getProduct_sku_id());
                epcEntity.setProduct_sku_name(CodeBind.getProduct_sku_name());
                epcEntity.setUnitId(CodeBind.getUnitId());
                epcEntity.setUnit_name(CodeBind.getUnit_name());
                epcEntity.setStockUnitId(CodeBind.getStockUnitId());
                epcEntity.setStock_unit_name(CodeBind.getStock_unit_name());
                epcEntity.setF_stock_count(CodeBind.getF_stock_count());
                epcEntity.setF_out_count(0.0d);
                epcEntity.setF_move_count(0.0d);
                //epcEntity.setInvalidDate(null);
                if (!StringUtils.isEmpty(CodeBind.getProductDate()))
                    epcEntity.setProductDate(CodeBind.getProductDate());
                list_epc.add(epcEntity);

                //二期方案 补录信息修改
                String tenantId = CodeBind.getTenant_id();
                String modifier = CodeBind.getModifier();
                StorerecordEntity storerecordEntity = new StorerecordEntity();
                storerecordEntity.setOrdercode(CodeBind.getOrdercode());//上游订单号
                storerecordEntity.setOrderstatus(CodeBind.getOrderstatus()); //上游单据状态
                storerecordEntity.setOrdertype(CodeBind.getOrdertype()); //单据类型
                storerecordEntity.setId(MyTool.getRandomId());
                storerecordEntity.setYtenant_id(tenantId);
                storerecordEntity.setRfidcodeId(epcid);
                storerecordEntity.setRfidcode(CodeBind.getRfid_code());
                storerecordEntity.setEpc_tid("");
                storerecordEntity.setOrderid(CodeBind.getOrderid());
                storerecordEntity.setOrderdetailid(CodeBind.getOrderdetailid());
                storerecordEntity.setStockId(CodeBind.getWarehouse_id());
                storerecordEntity.setCreate_time(MyTool.GenerateTime());
                storerecordEntity.setCreator(modifier);
                //方案处理
                storerecordEntity.setProduct_id(CodeBind.getProduct_id());
//                    storerecordEntity.setProduct_code(CodeBind.getProductcode());
                storerecordEntity.setProduct_name(CodeBind.getProduct_name());
                storerecordEntity.setProduct_sku_id(CodeBind.getProduct_sku_id());
//                    storerecordEntity.setProduct_sku_code(CodeBind.getProductskucode());
                storerecordEntity.setProduct_sku_name(CodeBind.getProduct_sku_name());
                storerecordEntity.setWarehouse_id(CodeBind.getWarehouse_id());
                storerecordEntity.setWarehouse_code(CodeBind.getWarehouse_code());
//                    storerecordEntity.setWarehouse_name(CodeBind.getWarehousename());
                storerecordEntity.setUnitId(CodeBind.getUnitId());
//                    storerecordEntity.setUnit_code(CodeBind.getUnitcode());
                storerecordEntity.setUnit_name(CodeBind.getUnit_name());
                storerecordEntity.setF_stock_count(CodeBind.getF_stock_count());
                storerecordEntity.setBatchno(CodeBind.getBatchno());
                storerecordEntity.setPickbatchno(batchNum);
                storerecordEntity.setPickstatus(DxqConfig.StatusOk);
                storerecordEntity.setBatchno_dxq(inbatchno);

                storerecordEntity.setStockUnitId(CodeBind.getStockUnitId());
                storerecordEntity.setStock_unit_id(CodeBind.getStockUnitId());
                storerecordEntity.setStock_unit_name(CodeBind.getStock_unit_name());
                storerecordEntity.setStock_unit_code("");
                //回写
                //storerecordEntity.setInvalidDate(null);
                if (!StringUtils.isEmpty(CodeBind.getProductDate()))
                    storerecordEntity.setProductDate(CodeBind.getProductDate());
                list_StorerecordEntity.add(storerecordEntity);
            }
            if (list_epc.size() == 0 || list_StorerecordEntity.size() == 0) {
                dataResult.setMessage("参数组织失败");
                return dataResult;
            }

            tempEpclistMapper.insertBatch(list_epc);
            tempStorerecordEntityMapper.insertBatch(list_StorerecordEntity);

            dataResult = new DataResult(1, "信息保存成功");

        } catch (Exception e) {
            dataResult = new DataResult(0, "信息保存失败!");
            dataResult.setMessage(e.getMessage());
        }

        return dataResult;
    }

    //拣货出库
//    @Transactional(rollbackFor = Exception.class)
//    public DataResult FahuoRFIDCode(List<PurOut> PurOutList, String tenantid) {
//        DataResult dataResult = new DataResult(0, "操作失败");
//        SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
//        EpclistMapper tempEpclistMapper = session.getMapper(EpclistMapper.class);
//        CoderfidMapper coderfidMappersel = session.getMapper(CoderfidMapper.class);
//        StorerecordEntityMapper tempStorerecordEntityMapper = session.getMapper(StorerecordEntityMapper.class);
//        String pickStatus = String.valueOf(DxqConfig.PickOk); //拣货状态
//        try {
//            if (PurOutList != null && PurOutList.size() > 0) {
//                List<String> coderfidArr = PurOutList.stream().map(e -> e.getRfidcodeId())
//                        .collect(Collectors.toList());
//                if (coderfidArr.size() > 0) {
//                    coderfidMappersel.Updatechukupick(coderfidArr, PurOutList.get(0).getCreator(),
//                            PurOutList.get(0).getCreator(), pickStatus, tenantid);
//                }
//                //获取库存信息
//                List<EpcEntity> list_epcEntity = tempEpclistMapper.selectByIds(coderfidArr, tenantid);
//                String ordertype = PurOutList.get(0).getOrdertype();
//                String batchNum = MyTool.GenerateBatchnoDXQ(ordertype);//私有批次
//                for (int i = 0; i < PurOutList.size(); i++) {
//                    PurOut CodeBind = PurOutList.get(i);
//                    String epcid = CodeBind.getRfidcodeId();
//                    EpcEntity tempEntity = list_epcEntity.stream().filter(it -> it.getId().equals(epcid))
//                            .findFirst().orElse(null);
//                    if (tempEntity == null) {
//                        dataResult.setMessage("物料不存在");
//                        return dataResult;
//                    }
//
////                    String warehouseId = CodeBind.getStockId();
////                    String Productid = CodeBind.getProductId();
////                    ProductEntity product = productMapper.selectById(Long.parseLong(Productid));
////                    String StockUnitId = tempEntity.getStockUnitId();
////
////                    if (product != null) {
////                        StockUnitId = product.getStockUnit();
////                    }
////                    String id = MyTool.getRandomId();
////                    String idcord = MyTool.getRandomId();
////                    rfidCodeDetailMapperall.InsertStorerecordOut(PurOutList.get(i).getOrdercode(),
////                            PurOutList.get(i).getOrderid(), PurOutList.get(i).getOrdertype(),
////                            Integer.parseInt(PurOutList.get(i).getOrderstatus()),
////                            PurOutList.get(i).getRfidcodeId(), PurOutList.get(i).getOrderdetailid(),
////                            PurOutList.get(i).getStockId(), StockUnitId,
////                            PurOutList.get(i).getPriceUOM(), PurOutList.get(i).getInvExchRate(),
////                            PurOutList.get(i).getUnitExchangeType(), PurOutList.get(i).getInvPriceExchRate(),
////                            PurOutList.get(i).getUnitExchangeTypePrice(),
////                            PurOutList.get(i).getSourceid(), PurOutList.get(i).getSourceautoid(),
////                            PurOutList.get(i).getSource(), PurOutList.get(i).getFirstsourceid(),
////                            PurOutList.get(i).getFirstsourceautoid(), PurOutList.get(i).getFirstsource(),
////                            PurOutList.get(i).getUpcode(), PurOutList.get(i).getFirstupcode(),
////                            PurOutList.get(i).getPoid(),
////                            PurOutList.get(i).getPodetailid(), PurOutList.get(i).getPocode(),
////                            PurOutList.get(i).getTenant_id(), "", 0, PurOutList.get(i).getCreator(),
////                            PurOutList.get(i).getModifier(), idcord);
//                    //二期方案 补录信息修改
//                    String tenantId = CodeBind.getTenant_id();
//                    String modifier = CodeBind.getModifier();
//
//                    StorerecordEntity storerecordEntity = new StorerecordEntity();
//                    storerecordEntity.setOrderid(CodeBind.getOrderid());
//                    storerecordEntity.setOrdercode(CodeBind.getOrdercode());//上游订单号
//                    storerecordEntity.setOrderstatus(Integer.parseInt(CodeBind.getOrderstatus())); //上游单据状态
//                    storerecordEntity.setOrdertype(CodeBind.getOrdertype()); //单据类型
//                    storerecordEntity.setId(MyTool.getRandomId());
//                    storerecordEntity.setTenant_id(tenantId);
//                    storerecordEntity.setRfidcodeId(epcid);
//                    storerecordEntity.setRfidcode(tempEntity.getRfid_code());
//                    storerecordEntity.setOrderdetailid(CodeBind.getOrderdetailid());
//                    storerecordEntity.setStockId(tempEntity.getWarehouse_id());
//                    storerecordEntity.setBatchno_dxq(batchNum);
//                    storerecordEntity.setCreate_time(MyTool.GenerateTime());
//                    storerecordEntity.setCreator(modifier);
//                    //方案处理
//                    storerecordEntity.setProduct_id(tempEntity.getProduct_id());
//                    storerecordEntity.setProduct_name(tempEntity.getProduct_name());
//                    storerecordEntity.setProduct_sku_id(tempEntity.getProduct_sku_id());
//                    storerecordEntity.setProduct_sku_name(tempEntity.getProduct_sku_name());
//                    storerecordEntity.setWarehouse_id(tempEntity.getWarehouse_id());
//                    storerecordEntity.setWarehouse_code(tempEntity.getWarehouse_code());
//                    storerecordEntity.setUnitId(tempEntity.getUnitId());
//                    storerecordEntity.setUnit_name(tempEntity.getUnit_name());
//                    storerecordEntity.setF_stock_count(tempEntity.getF_stock_count());
//                    storerecordEntity.setStockUnitId(tempEntity.getStockUnitId());
//                    storerecordEntity.setStock_unit_id(tempEntity.getStockUnitId());
//                    storerecordEntity.setStock_unit_code("");
//                    storerecordEntity.setStock_unit_name(tempEntity.getStock_unit_name());
//                    storerecordEntity.setBatchno(tempEntity.getBatchno());
//                    storerecordEntity.setEpc_tid(tempEntity.getEpc_tid());
//                    //拣货回写
//                    storerecordEntity.setEpc_status(Integer.parseInt(pickStatus));
//                    storerecordEntity.setLocationId(tempEntity.getLocation_id());
//                    if (!StringUtils.isEmpty(tempEntity.getProductDate()))
//                        storerecordEntity.setProductDate(tempEntity.getProductDate());
//                    if (!StringUtils.isEmpty(tempEntity.getInvalidDate()))
//                        storerecordEntity.setInvalidDate(tempEntity.getInvalidDate());
//
//                    //回写是否回写
////                    storerecordEntity.setPoid(CodeBind.getPoid());
////                    storerecordEntity.setPodetailid(CodeBind.getPodetailid());
////                    storerecordEntity.setPocode(CodeBind.getPocode());
//                    //原
//                    storerecordEntity.setPriceUOM(CodeBind.getPriceUOM());
//                    storerecordEntity.setPriceUOM(CodeBind.getPriceUOM());
//                    storerecordEntity.setPriceUOM(CodeBind.getInvExchRate());
//                    storerecordEntity.setPriceUOM(CodeBind.getUnitExchangeType());
//                    storerecordEntity.setPriceUOM(CodeBind.getInvPriceExchRate());
//                    storerecordEntity.setPriceUOM(CodeBind.getUnitExchangeTypePrice());
//
//                    tempStorerecordEntityMapper.insert(storerecordEntity);
//                    if (i % 1000 == 999) {//每1000条提交一次防止内存溢出
//                        session.commit();
//                        session.clearCache();
//                    }
//                }
//                session.commit();
//                session.clearCache();
//                dataResult = new DataResult(1, "信息拣货成功");
//            } else {
//                dataResult = new DataResult(0, "提交的数据条数为空!");
//            }
//
//        } catch (Exception e) {
//            dataResult = new DataResult(0, "信息拣货失败!");
//            //e.printStackTrace();
//            session.clearCache();
//            return dataResult;
//        }
//
//        return dataResult;
//    }

    //删除拣货
    @Transactional(rollbackFor = Exception.class)
    public DataResult DelJianhuo(String id, String rfidcodeId, String modifier, String tenantid) {
        DataResult dataResult = new DataResult(0, "参数异常");
        List<String> rfidcodeIdsList = new ArrayList<>();
        List<String> idsList = new ArrayList<>();
        try {
            if (rfidcodeId.length() > 0) {
                String[] strings = rfidcodeId.split(",");
                rfidcodeIdsList.addAll(Arrays.asList(strings));
            }
            if (id.length() > 0) {
                String[] strings = id.split(",");
                idsList.addAll(Arrays.asList(strings));
            }
            // 如果为空的话增加一个空值
            if (rfidcodeIdsList.size() == 0 || rfidcodeIdsList == null || idsList.size() == 0 || idsList == null) {
                return dataResult;
            }
            //EPC 批量更新
            coderfidMappersel.Updatechuku(rfidcodeIdsList, modifier, "1", tenantid);
            //Storerecord
            rfidCodeDetailMapperall.deleteStorerecordByid(idsList, modifier, tenantid);

            dataResult = new DataResult(1, "删除拣货信息成功");
        } catch (Exception e) {
            dataResult = new DataResult(0, "删除拣货信息失败!");
            //e.printStackTrace();
            return dataResult;
        }
        return dataResult;
    }

    //查询库存数量
    public DataResult GetKucunCount(String product_id, String product_sku_id, String warehouse_id, String tenantid) {
        DataResult dataResult = null;
        try {
            List<coderfid> coderfidList = coderfidMappersel.SelcoderfidList(product_id, product_sku_id, warehouse_id,
                    tenantid);
            if (coderfidList != null && coderfidList.size() > 0) {
                double checknumCount = coderfidList.stream().mapToDouble(coderfid::getF_stock_count).sum();
                dataResult = new DataResult(1, "信息查询成功！", checknumCount);
            } else {
                dataResult = new DataResult(1, "信息查询成功！", 0);
            }
        } catch (Exception e) {
            dataResult = new DataResult(0, "查询信息失败!");
            //e.printStackTrace();
        }
        return dataResult;
    }

    //解绑标签 到货发签解绑
    @Transactional(rollbackFor = Exception.class)
    public DataResult JieBindRfidCode(String id, String modifier, String tenantid) {
        DataResult dataResult = null;
        try {
            if (id != null && !id.equals("")) {
                storeRecord storeRecord = rfidCodeDetailMapperall.selectByid(id, tenantid);
                if (storeRecord != null) {
                    dataResult = new DataResult(0, "要删除的物料标签已到货提交,不允许删除！");
                    return dataResult;
                }
                coderfidMappersel.deleteCoderfid(id, modifier, tenantid);
                rfidCodeDetailMapperall.deleteStorerecord(id, modifier, tenantid);
                dataResult = new DataResult(1, "标签删除成功");
            } else {
                dataResult = new DataResult(0, "参数错误");
            }
        } catch (Exception e) {
            dataResult = new DataResult(0, "标签删除失败!");
            // e.printStackTrace();
            return dataResult;
        }
        return dataResult;
    }

    //下架标签货位信息
    public DataResult CleanReRFIDCode(String id, String modifier, String tenantid) {
        DataResult dataResult = null;
        try {
            if (id != null && !id.equals("")) {
                int updateID = coderfidMappersel.CleanCoderfid(id, modifier, tenantid);
                if (updateID > 0) {
                    dataResult = new DataResult(1, "标签清除货位信息成功");
                } else {
                    dataResult = new DataResult(0, "标签清除货位信息失败或者当前标签不存在!");
                }
            } else {
                dataResult = new DataResult(0, "参数错误!");
            }
        } catch (Exception e) {
            dataResult = new DataResult(0, "标签清除货位信息失败!");
            //e.printStackTrace();
        }
        return dataResult;
    }


    /**
     * 对标签商品上架 不能将货品上架到货架上
     * 升级改for循环更新数据 by chen
     *
     * @param id       epcids
     * @param localID  货位Id
     * @param modifier
     * @param tenantid
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public DataResult EditEpcForShangjia(String id, String localID, String ordertype, String modifier, String tenantid) throws Exception {
        DataResult dataResult = new DataResult(0, "参数异常");
        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(localID)) {
            dataResult = new DataResult(0, "参数错误!");
            return dataResult;
        }
        List<String> ids = Arrays.asList(id.split(",")).stream().filter(it -> !it.isEmpty()).distinct().collect(Collectors.toList());
        if (ids.size() == 0) return dataResult;
        // 修改Epc数据信息
        LocationEntity location = locationEntityService.getLocationById(localID, tenantid);
        if (location == null) return dataResult;
        String huojiaID = LocationEntityService.GetHuojiaIDByEntity(location, tenantid);

        List<EpcEntity> epcEntityList = tempEpclistMapper.selectByIds(ids, tenantid);
        List<String> epcIds_EditLocationId = new ArrayList<>();
        for (EpcEntity item : epcEntityList) {
            if (StringUtils.isEmpty(item.getLocation_id())) epcIds_EditLocationId.add(item.getId());
            item.setShelf_id(huojiaID);
            item.setLocation_id(location.getId());
            item.setWarehouse_id(location.getWarehouse_id());
            item.setWarehouse_code(location.getWarehouse_code());
            item.setModifier(modifier);
            item.setRfid_status(1);
        }
        if (epcEntityList.size() > 0) {
            // epcEntityList 实体设置状态 rfid_status=1
            int rInt = tempEpclistMapper.editForShangjia(epcEntityList, tenantid);
        }
        if (!StringUtils.isEmpty(ordertype) && epcIds_EditLocationId.size() > 0) {
            tempStorerecordEntityMapper.editLocationId(epcIds_EditLocationId, ordertype, localID, tenantid);
        }
        dataResult = new DataResult(1, "操作成功");
        return dataResult;
    }

    /**
     * 根据RfidCode 查询标签是否被占用
     * 批量检查
     *
     * @param rfidCode rfidCode epc逗号分隔
     * @return 返回是否被占用结果信息 如果被占用返回占用物资名称
     * @throws Exception
     */

    public DataResult checkRifdOccupy(String rfidCode, String orderId, String ordertype, String tenantid) throws Exception {
        List<coderfid> rfidCodeList;
        DataResult dataResult = new DataResult(1, "标签未被占用");

        if (orderId.length() > 0) {
            OrderNoticeEntity ordernotice = guiZeMapper.selectOrderNoticeByIdAndType(orderId, ordertype);
            if (ordernotice != null) {
                dataResult.setOrderAuditType(ordernotice.getAuditstatus());
            }
        }
        try {
            List<String> strList = Arrays.asList(rfidCode.split(","));
            if (strList == null || strList.size() == 0) {
                return dataResult;
            }
            rfidCodeList = rfidCodeDetailMapperall.selectByEpcs(strList, tenantid);
            // 如果存在被占用的情况
            if (rfidCodeList != null && rfidCodeList.size() > 0) {
                dataResult.setStatus(0);
                dataResult.setMessage("标签已经被占用，物料名称为：" + rfidCodeList.get(0).getProduct_name());
                return dataResult;
            } else {
                return dataResult;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataResult;
    }
}



